#!/bin/bash 

bold=$(tput bold)
normal=$(tput sgr0)

echo "Make sure you do provision under ${bold}DEVELOP${normal} branch!!"

echo "--> Checking if the files are Unix style"

[[ $(file functions.sh) =~ CRLF ]] && echo "Windows style. Make sure git's autocrlf is set to false before proceed!!" && exit 1

echo "--> Starting Vagrant Pre-provisioning: pre_provision.sh..." 

SOURCE_ROOT=..

# First thing first, since we are using YUM caching inside Vagrant (Very Important), let's
# disable YUM CLEAN as soon as possible.
if [ ! -e /usr/share/yum-cli/cli.py.bak ]; then
     myline="/if 'all' in userlist:/"
     mytext="\
\ \ \ \ \ \ \ \ # Added by stevel, Nov 29, 2014\n\
\ \ \ \ \ \ \ \ return 1, [_('YUM CLEAN disabled inside Vagrant at Sumilux, see #7712 ')]"
     sudo sed -i.bak -e "${myline} i${mytext}" /usr/share/yum-cli/cli.py
fi;

#Add dns
#dns=$(cat /etc/resolv.conf | grep "nameserver 8.8.8.8")
#if [ -z "$dns" ]; then
#	sudo echo "nameserver 8.8.8.8" > /etc/resolv.conf
#	sudo echo "nameserver 8.8.4.4" >> /etc/resolv.conf
#	sudo echo "nameserver 114.114.114.114" >> /etc/resolv.conf 
#fi

# Setting YUM architecture "i386" or "x86_64" for later repo operations
YUM_BASEARCH=`uname -i`
YUM_ARCH=`uname -m`

# Add epel repo
echo "--> Installing EPEL repo"
if [ ! -e /etc/yum.repos.d/epel.repo ]; then
   echo "Installing EPEL repo..."
   # Note, $YUM_BASEARCH defined in function.sh
   if [ ! -e /etc/os-release ]; then
       sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/${YUM_BASEARCH}/epel-release-6-8.noarch.rpm
   else
       sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/${YUM_BASEARCH}/e/epel-release-7-5.noarch.rpm
       sudo rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
       sudo sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/mysql-community.repo
       echo "mysql repo installed"
   fi
   # Never CLEAN YUM any more, see below
   # NEED_YUM_CLEAN=1
   echo "EPEL repo installed"
fi

# Add mysql repo
#echo "--> Installing Mysql repo"
#if [ ! -e /etc/yum.repos.d/mysql-community.repo ]; then
#   echo "Installing MYSQL repo..."
#   # Note, $YUM_BASEARCH defined in function.sh
#   if [ ! -e /etc/os-release ]; then
#      echo "no need to install in centos 6"
#   else
#       sudo rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
#       sudo sed -i "s/enabled=0/enabled=1/g" /etc/yum.repos.d/mysql-community.repo
#   fi
#   # Never CLEAN YUM any more, see below
#   # NEED_YUM_CLEAN=1
#   echo "MYSQL repo installed"
#fi

# Clean up previous installations
if  [ -e /etc/yum.repos.d/CentOS-6-Vagrant**.repo ]; then
  rm -f /etc/yum.repos.d/CentOS-6-Vagrant**.repo
fi

echo "--> Installating Sumilux YUM REPO collection"
#smx-collection is under git track , need to copy whatever the smx-collection.repo is exist
#if [ ! -e /etc/yum.repos.d/smx-collection.repo ]; then
   cp $SOURCE_ROOT/vagrant/repo/smx-collection.repo /etc/yum.repos.d
   echo "smx-collection custom repos added"
#fi

echo "--> Install Puppet"
if rpm -aq|grep -q puppet; then
   echo "Puppet ready"
else
   yum install -y puppet
fi

echo "--> Install Rsync"
if rpm -aq|grep -q rsync; then
   echo "Rsync installed"
else
   yum install -y rsync
fi

echo "--> Install Wget"
if rpm -aq|grep -q wget; then
   echo "Wget installed"
else
   yum install -y wget
fi

# add host ip for centos 7
if !(grep -q vagrant.foxitcloud.com /etc/hosts); then
  echo "192.168.65.19 form.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 epdfform.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 dococloud.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 v.doc.sh" >> /etc/hosts
  echo "192.168.65.19 admin.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 xapi.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 www.vagrant.foxitcloud.com v.fxcld.net" >> /etc/hosts
  echo "192.168.65.19 forms.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 c71.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 mongo.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 platform.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 worker.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 s3.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 es.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 mongodb.vagrant.foxitcloud.com" >> /etc/hosts
  echo "192.168.65.19 gearman.vagrant.foxitcloud.com" >> /etc/hosts

fi

echo "--> Pre_provision finished in ${SECONDS}s"
